﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;

namespace DelicateDict.Classes
{
    class SearchWord
    {
        public int left, right;
        public int Index = 0;
        public int OldIndex;
        CompareInfo ci = CultureInfo.GetCultureInfo("").CompareInfo;
        List<string> ArrayWord = null;
        public SearchWord()
        {
            WordList wl = new WordList();
            ArrayWord = wl.ArrayWord;
        }
        public SearchWord(DictType dt)
        {
            IOFile iof = new IOFile();
            ArrayWord = iof.GetAllWordFromDictType(dt);
        }
        public SearchWord(List<string> l)
        {

            ArrayWord = l;
        }

        public void CloseSearchWord()
        {
            ArrayWord = null;
        }
        public bool GetIndexSearch(string input)
        {
            if (ArrayWord.Count < 2)
                return false;
            Index = this.BinarySearch(input);
            return true;
        }
        private int BinarySearch(string word1)
        {
            left = 0;
            right = ArrayWord.Count - 2;
            do
            {
                int mid = (left + right) / 2;
                string word2 = ArrayWord[mid].ToString();
                int compare = ci.Compare(word1.Trim().ToLower(), word2.Trim().ToLower(), CompareOptions.Ordinal);
                //int compare = String.Compare(word1.Trim().ToLower(),word2.Trim().ToLower(),true,new CultureInfo("vi-Vn"));
                if (compare == 0)
                {
                    OldIndex = mid;
                    return mid;
                }
                if (compare > 0) left = mid + 1;
                else right = mid - 1;
            } while (left <= right);
            return -1;
        }
    }
}
